package com.example.sachon;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListContentListAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<ListContentItem> list;
	private final String LOGTAG = "ListConAdapter_";

	public ListContentListAdapter(Context context, List<ListContentItem> list) {
		inflater = LayoutInflater.from(context);
		this.list = list;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	public long getItemId(int position) {

		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;

		Log.d(LOGTAG, " getView : " + position);

		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.first_list_row, null);

			viewHolder.listNo = (TextView) convertView.findViewById(R.id.listNo);
			viewHolder.listSubject = (TextView) convertView.findViewById(R.id.listSubject);

			viewHolder.replyImg = (ImageView) convertView.findViewById(R.id.replyImg);
			viewHolder.replyNo = (TextView) convertView.findViewById(R.id.replyNo);
			
			viewHolder.image1 = (ImageView) convertView.findViewById(R.id.image1);
			viewHolder.image2 = (ImageView) convertView.findViewById(R.id.image2);
			viewHolder.image3 = (ImageView) convertView.findViewById(R.id.image3);
			viewHolder.image4 = (ImageView) convertView.findViewById(R.id.image4);
			//viewHolder.image5 = (ImageView) convertView.findViewById(R.id.image5);
			
			convertView.setTag(viewHolder);

			
			Log.d(LOGTAG, " convertView is Not Null ");
		} else {
			viewHolder = (ViewHolder) convertView.getTag();			
		}
		
		viewHolder.image1.setVisibility(viewHolder.image1.GONE);
		viewHolder.image2.setVisibility(viewHolder.image2.GONE);
		viewHolder.image3.setVisibility(viewHolder.image3.GONE);
		viewHolder.image4.setVisibility(viewHolder.image4.GONE);
		//viewHolder.image5.setVisibility(viewHolder.image5.GONE);
		
		viewHolder.replyImg.setVisibility(viewHolder.replyImg.GONE);
		viewHolder.replyNo.setText("");
		
		try {
			viewHolder.listNo.setText(list.get(position).getUser_name() + "\n" + list.get(position).getWr_dt());
			
			if(Integer.parseInt(list.get(position).getCnt()) > 0){
				viewHolder.replyImg.setImageResource(R.drawable.reply_icon);
				viewHolder.replyImg.setVisibility(viewHolder.replyImg.VISIBLE);
				viewHolder.replyNo.setText(list.get(position).getCnt());
			}
			
			viewHolder.listSubject.setText(list.get(position).getContent());

			//이미지 보이기
			if (list.get(position).getPic_names().trim().length() > 0) {
				String[] imageArray = list.get(position).getPic_names().trim().split(";");
				
				Log.d(LOGTAG, " imageArray : " + imageArray.length);

				for (int i = 0; i < imageArray.length; i++) {
					try{
						String imageName = imageArray[i].toString().trim();
						
						String urlString = "http://auralizer.cafe24.com/upload/" + imageName;
						Log.d(LOGTAG, "Img URL : "+urlString);
						Log.d(LOGTAG, position + "행의 " + i + " 번째 이미지 : " + imageName);
						
						if( i == 0){
							
							ImageDownloader.download(urlString, viewHolder.image1);
							
							viewHolder.image1.setVisibility(viewHolder.image1.VISIBLE);
							
						}else if( i == 1 ){
							ImageDownloader.download(urlString, viewHolder.image2);
							viewHolder.image2.setVisibility(viewHolder.image2.VISIBLE);
						}else if( i == 2 ){
							ImageDownloader.download(urlString, viewHolder.image3);
							viewHolder.image3.setVisibility(viewHolder.image3.VISIBLE);
						}else if( i == 3 ){
							ImageDownloader.download(urlString, viewHolder.image4);
							viewHolder.image4.setVisibility(viewHolder.image4.VISIBLE);
						}/*else if( i == 4 ){
							ImageDownloader.download(urlString, viewHolder.image5);
							viewHolder.image5.setVisibility(viewHolder.image5.VISIBLE);
						}*/
					} catch (Exception imageE) {
						imageE.printStackTrace();
					}
				}
			}			

		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;

	}

	public final class ViewHolder {
		
		
		private TextView listNo;
		private TextView listSubject;
		private ImageView replyImg;
		private TextView replyNo;		
		private ImageView image1;
		private ImageView image2;
		private ImageView image3;
		private ImageView image4;
		private ImageView image5;
	}

}